package com.apa7.keepaccounts.controller;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.apa7.keepaccounts.dto.ReturnData;
import com.apa7.keepaccounts.service.RecordService;
import com.apa7.keepaccounts.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    /**
     * 记录一笔钱
     * @return ReturnData
     */
    @RequestMapping("/set")
    public ReturnData set(){
        try {
            JSONObject param= Tool.getJsonOject();
            if(param.getString("key").equals(""))
                return new ReturnData("缺少参数");
            if(param.getString("payMode").equals(""))
                return new ReturnData("缺少参数");
            if(param.getString("payType").equals(""))
                return new ReturnData("缺少参数");
            if(param.getString("payTypeId").equals(""))
                return new ReturnData("缺少参数");

            int result=recordService.set(
                    param.getString("key"),
                    param.getIntValue("money"),
                    param.getIntValue("payTypeId"),
                    param.getString("payType"),
                    param.getString("payMode"),
                    param.getLong("date")
            );

            if(result==0)
                return new ReturnData("添加失败");

            return new ReturnData();
        }catch (JSONException | NumberFormatException e){
            return new ReturnData("参数错误！");
        }
    }

    @RequestMapping("/update")
    public ReturnData update(){
        System.out.println("你好");
        try {
            JSONObject param= Tool.getJsonOject();
            if(param.getString("key").equals(""))
                return new ReturnData("缺少参数");
            if(param.getString("payMode").equals(""))
                return new ReturnData("缺少参数");
            if(param.getString("payType").equals(""))
                return new ReturnData("缺少参数");
            if(param.getString("payTypeId").equals(""))
                return new ReturnData("缺少参数");

            String result=recordService.update(
                    param.getString("key"),
                    param.getIntValue("billId"),
                    param.getIntValue("money"),
                    param.getIntValue("payTypeId"),
                    param.getString("payType"),
                    param.getString("payMode"),
                    param.getLong("date")
            );

            if(!result.equals(""))
                return new ReturnData(result);

            return new ReturnData();
        }catch (JSONException | NumberFormatException e){
            return new ReturnData("参数错误！");
        }
    }

    @RequestMapping("/delete")
    public ReturnData delete(){
        try {
            JSONObject param= Tool.getJsonOject();
            if(param.getString("key").equals(""))
                return new ReturnData("缺少参数");

            String result=recordService.delete(
                    param.getString("key"),
                    param.getIntValue("billId")
            );

            if(!result.equals(""))
                return new ReturnData(result);

            return new ReturnData();
        }catch (JSONException | NumberFormatException e){
            return new ReturnData("参数错误！");
        }
    }
}
